package com.yangyuhao.haowuyoupin.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yangyuhao.haowuyoupin.model.User;

public class UserDao {
    private DBHelper dbHelper;

    public UserDao(Context context) {
        dbHelper = new DBHelper(context);
    }

    // 添加用户
    public long addUser(String account, String password, String nickname) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_ACCOUNT, account);
        values.put(DBHelper.COLUMN_PASSWORD, password);
        values.put(DBHelper.COLUMN_NICKNAME, nickname);
        long result = db.insert(DBHelper.TABLE_USERS, null, values);
        db.close();
        return result;
    }

    // 更新用户登录状态
    public boolean updateLoginStatus(String account, boolean isLogin) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_IS_LOGIN, isLogin ? 1 : 0);
        int result = db.update(DBHelper.TABLE_USERS, values, 
            DBHelper.COLUMN_ACCOUNT + "=?", new String[]{account});
        db.close();
        return result > 0;
    }

    // 获取当前登录用户
    public User getCurrentUser() {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_USERS, null, 
            DBHelper.COLUMN_IS_LOGIN + "=?", new String[]{"1"}, 
            null, null, null);
        
        User user = null;
        if (cursor.moveToFirst()) {
            user = new User();
            // 获取列索引
            int accountIndex = cursor.getColumnIndexOrThrow(DBHelper.COLUMN_ACCOUNT);
            int nicknameIndex = cursor.getColumnIndexOrThrow(DBHelper.COLUMN_NICKNAME);
            int creditScoreIndex = cursor.getColumnIndexOrThrow(DBHelper.COLUMN_CREDIT_SCORE);
            int isAdminIndex = cursor.getColumnIndexOrThrow(DBHelper.COLUMN_IS_ADMIN);

            // 使用列索引获取数据
            user.setAccount(cursor.getString(accountIndex));
            user.setNickname(cursor.getString(nicknameIndex));
            user.setCreditScore(cursor.getInt(creditScoreIndex));
            user.setAdmin(cursor.getInt(isAdminIndex) == 1);
        }
        cursor.close();
        db.close();
        return user;
    }

    // 更新用户信息
    public boolean updateUser(User user) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(DBHelper.COLUMN_NICKNAME, user.getNickname());
        values.put(DBHelper.COLUMN_CREDIT_SCORE, user.getCreditScore());
        values.put(DBHelper.COLUMN_EXTRA, user.getExtra());
        
        int result = db.update(DBHelper.TABLE_USERS, values, 
            DBHelper.COLUMN_ACCOUNT + "=?", new String[]{user.getAccount()});
        db.close();
        return result > 0;
    }

    // 验证用户登录
    public boolean verifyLogin(String account, String password) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query(DBHelper.TABLE_USERS, null, 
            DBHelper.COLUMN_ACCOUNT + "=? AND " + DBHelper.COLUMN_PASSWORD + "=?", 
            new String[]{account, password}, null, null, null);
        
        boolean result = cursor.moveToFirst();
        cursor.close();
        db.close();
        return result;
    }

    // 修改用户密码
    public boolean updatePassword(String account, String oldPassword, String newPassword) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        
        // 先验证旧密码是否正确
        Cursor cursor = db.query(DBHelper.TABLE_USERS, null,
            DBHelper.COLUMN_ACCOUNT + "=? AND " + DBHelper.COLUMN_PASSWORD + "=?",
            new String[]{account, oldPassword}, null, null, null);
        
        if (cursor.moveToFirst()) {
            ContentValues values = new ContentValues();
            values.put(DBHelper.COLUMN_PASSWORD, newPassword);
            
            int result = db.update(DBHelper.TABLE_USERS, values,
                DBHelper.COLUMN_ACCOUNT + "=?", new String[]{account});
                
            cursor.close();
            db.close();
            return result > 0;
        }
        
        cursor.close();
        db.close();
        return false;
    }
} 